package com.family.controller;

import com.family.model.*;
import com.family.model.result.ResultModel;
import com.family.model.result.StateCode;
import com.family.service.GenerationService;
import com.family.service.NameService;
import com.family.service.RelationshipService;
import com.family.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 操作与姓、字辈相关操作
 */
@RestController
@RequestMapping("/name")
public class NameController {

    private NameService nameService;
    private UserService userService;
    private GenerationService generationService;
    private RelationshipService relationshipService;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public void setNameService(NameService nameService) {
        this.nameService = nameService;
    }

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public void setGenerationService(GenerationService generationService) {
        this.generationService = generationService;
    }

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public void setRelationshipService(RelationshipService relationshipService) {
        this.relationshipService = relationshipService;
    }

    /**
     * @param userId userId
     * @param name   姓
     * @return
     */
    @RequestMapping(value = "/addName.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel<Name> addName(int userId, String name) {
        ResultModel<Name> resultModel = new ResultModel<>();
        Name nameModel = nameService.getNameByName(name);
        if (nameModel != null) {
            resultModel.setData(nameModel);
            return resultModel;
        } else if (userId <= 0) {//用户不存在
            resultModel.setPrompt("用户id错误");
            resultModel.setStateCode(StateCode.OTHER_ERROR);
            return resultModel;
        } else {
            User user = userService.getUserById(userId);
            if (user != null) {
                nameModel = new Name();
                nameModel.setName(name);
                nameModel.setUser(user);
                nameService.addName(nameModel);
                resultModel.setData(nameModel);
                return resultModel;
            } else {
                resultModel.setPrompt("用户不存在");
                resultModel.setStateCode(StateCode.OTHER_ERROR);
                return resultModel;
            }
        }
    }

    /**
     * 根据姓id查询字辈
     *
     * @param nameId
     * @return
     */
    @RequestMapping(value = "/queryGenerationByNameId.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel<List<Generation>> queryGenerationByNameId(int nameId) {
        ResultModel<List<Generation>> resultModel = new ResultModel<>();
        resultModel.setData(generationService.queryGenerationsByNameId(nameId));
        return resultModel;
    }

    /**
     * 添加字辈
     *
     * @param userId             当前用户id
     * @param generation         字辈
     * @param parentGenerationId 父字辈
     */
    @RequestMapping(value = "/addGeneration.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel<Generation> addGeneration(int userId, int nameId, String generation, int parentGenerationId) {
        User user = userService.getUserById(userId);
        Name name = nameService.getNameById(nameId);
        ResultModel<Generation> resultModel = new ResultModel<>();
        if (user == null ){
            resultModel.setStateCode(StateCode.OTHER_ERROR);
            resultModel.setPrompt("用户不存在");
        }else if(name == null){
            resultModel.setStateCode(StateCode.OTHER_ERROR);
            resultModel.setPrompt("姓氏不存在");
        }else {
            Generation generationModel = generationService.queryGenerationByName(nameId, generation);
            if (generationModel == null) {
                List<Generation> currents = generationService.queryGenerationsByParentId(parentGenerationId);
                generationModel = new Generation();
                generationModel.setValue(generation);
                generationModel.setName(name);
                generationModel.setUser(user);
                generationModel.setParentGeneration(generationService.queryGenerationById(parentGenerationId));
                Generation newGeneration = generationService.addGeneration(generationModel);
                if (currents != null && currents.size() > 0) {
                    for (Generation temp : currents) {
                        temp.setParentGeneration(newGeneration);
                    }
                    generationService.updateGenerations(currents);
                }
                resultModel.setData(newGeneration);
            } else {
                resultModel.setData(generationModel);
            }
        }

        return resultModel;
    }

    /**
     * 查询关系
     *
     * @return
     */
    @RequestMapping(value = "/queryRelationships.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel<List<RelationShipState>> queryRelationships() {
        ResultModel<List<RelationShipState>> resultModel = new ResultModel();
        resultModel.setData(relationshipService.queryRelationships());
        return resultModel;
    }

    /**
     * 删除字辈
     *
     * @param userId
     * @param generationId
     * @return
     */
    @RequestMapping(value = "deleteGeneration.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultModel<List<Generation>> deleteGeneration(int userId, int generationId) {
        Generation generation = generationService.queryGenerationById(generationId);
        ResultModel resultModel = new ResultModel();
        if (generation == null) {
            resultModel.setStateCode(StateCode.PARAMS_ERROR);
            resultModel.setPrompt("字辈不存在");
        } else if (generation.getUser() != null && userId != generation.getUser().getId()) {
            resultModel.setStateCode(StateCode.PARAMS_ERROR);
            resultModel.setPrompt("您没有权限删除字辈,请联系添加字辈的人");
        } else {//TODO 删除字辈
            List<Generation> generations = generationService.queryGenerationsByParentId(generation.getId());
            if (generations != null && generations.size() > 0) {
                for (Generation temp : generations) {
                    temp.setParentGeneration(generation.getParentGeneration());
                }
                generationService.updateGenerations(generations);
            }
            generationService.deleteGenerationById(generationId);
            if (generation.getName() != null) {
                resultModel.setData(generationService.queryGenerationsByNameId(generation.getName().getId()));
            }
        }
        return resultModel;
    }

}
